﻿using System.Collections.Generic;
using System.ComponentModel;
using Microsoft.Practices.Composite.Events;
using VisualStock.Infrastructure;
using VisualStock.Infrastructure.Interfaces;
using VisualStock.Modules.StockMarket.HistoricalData.Interfaces;
using VisualStock.Modules.StockMarket.Models;
using Microsoft.Practices.Composite.Presentation.Events;
using VisualStock.Infrastructure.Models;
using Microsoft.Practices.Composite.Presentation.Commands;
using System.IO.IsolatedStorage;

namespace VisualStock.Modules.StockMarket.HistoricalData
{
    public class HistoricalDataMenuViewModel : IHistoricalDataMenuViewModel, INotifyPropertyChanged
    {
        private readonly IEventAggregator _eventAggregator;

        private readonly IHistoricalDataService _historicalDataService;

        private DelegateCommand<object> _saveCommand;

        private DelegateCommand<object> _loadCommand;

        private TechnicalIndicatorSetting _currentTechSetting = new TechnicalIndicatorSetting();

        public List<string> CurrentSymbolList { get; set; }

        public DelegateCommand<object> SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new DelegateCommand<object>(OnSaveExcuted, CanSaveExcuted);
                }
                return _saveCommand;
            }
        }

        public DelegateCommand<object> LoadCommand
        {
            get
            {
                if (_loadCommand == null)
                {
                    _loadCommand = new DelegateCommand<object>(OnLoadExcuted);
                }
                return _loadCommand;
            }
        }

        public HistoricalDataMenuViewModel(IHistoricalDataMenuView view, IHistoricalDataService historicalDataService, ISettingController observableSettings, IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
            _historicalDataService = historicalDataService;
            this.CurrentChartSettings = observableSettings;
            this.View = view;
            this.View.Model = this;

            _eventAggregator.GetEvent<NewComparisonRequestedEvent>().Subscribe(UpdateCurrentSymbolList, ThreadOption.UIThread);
        }

        public void UpdateCurrentSymbolList(List<string> newSymbolList)
        {
            CurrentSymbolList = newSymbolList;
            _saveCommand.RaiseCanExecuteChanged();
            this.InvokePropertyChanged("CurrentSymbolList");
        }

        private bool CanSaveExcuted(object parameter)
        {
            if (CurrentSymbolList == null || CurrentSymbolList.Count < 1) return false;
            return true;
        }

        private void OnSaveExcuted(object parameter)
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (store.AvailableFreeSpace < store.Quota / 2 && !store.IncreaseQuotaTo(store.Quota * 2)) return;
                _historicalDataService.SaveHistoricalData(CurrentSymbolList[0]);
            }
        }

        private void OnLoadExcuted(object parameter)
        {
            _historicalDataService.LoadAllHistoricalData();
        }
        #region IHistoricalDataMenuViewModel Members

        public IHistoricalDataMenuView View { get; set; }

        public ISettingController CurrentChartSettings { get; private set; }

        public event PropertyChangedEventHandler TechSettingChanged = delegate { };

        #endregion

        public event PropertyChangedEventHandler PropertyChanged;

        private void InvokePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler Handler = PropertyChanged;
            if (Handler != null) Handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
